package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.rewarddistribution.RewardDistributionAddDto;
import com.teacher.pojo.dto.rewarddistribution.RewardDistributionGetDto;
import com.teacher.pojo.dto.rewarddistribution.RewardDistributionUpdateDto;
import com.teacher.pojo.entity.RewardDistribution;
import com.teacher.pojo.vo.rewarddistribution.RewardDistributionGetVo;
import com.teacher.service.IRewardDistributionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 奖学金分配表 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-23
 */
@RestController
@RequestMapping("/pc/reward-distribution")
public class RewardDistributionController {

    @Autowired
    private IRewardDistributionService iRewardDistributionService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加奖学金分配
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加奖学金分配 rewardDistribution_add")
    @PostMapping
    @PreAuthorize("hasAuthority('rewardDistribution_add')")
    public CommonResult<String> add(@RequestBody @Validated RewardDistributionAddDto dto) {
        RewardDistribution rewardDistribution = new RewardDistribution();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, rewardDistribution);
        if (iRewardDistributionService.save(rewardDistribution)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新奖学金分配
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新奖学金分配 rewardDistribution_update")
    @PutMapping
    @PreAuthorize("hasAuthority('rewardDistribution_update')")
    public CommonResult<String> update(@RequestBody @Validated RewardDistributionUpdateDto dto) {
        RewardDistribution rewardDistribution = new RewardDistribution();
        BeanUtils.copyProperties(dto, rewardDistribution);
        if (iRewardDistributionService.updateById(rewardDistribution)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除奖学金分配
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除奖学金分配 rewardDistribution_delete")
    @PreAuthorize("hasAuthority('rewardDistribution_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iRewardDistributionService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询奖学金分配
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询奖学金分配 rewardDistribution_get")
    @PreAuthorize("hasAuthority('rewardDistribution_get')")
    public CommonResult<IPage<RewardDistributionGetVo>> getPage(RewardDistributionGetDto dto) {
        return iRewardDistributionService.getPage(dto);
    }

    /**
     * 导入奖学金分配
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入奖学金分配 rewardDistribution_upload")
    @PreAuthorize("hasAuthority('rewardDistribution_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iRewardDistributionService.upload(file);
    }


}

